package com.app.business.service.uni;

import com.app.business.model.Contract;
import com.app.system.jpa.JpaBaseDao;
import com.app.system.jpa.PageBean;
import com.app.system.jpa.Wrapper;
import com.app.system.utils.ParamUtils;
import com.app.system.utils.dataType.StringUtils;
import com.google.common.collect.Maps;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 手机端
 * 选房号业务层
 *
 * @author lsh
 */
@SuppressWarnings(value = "ALL")
@Service
public class AppContractService extends JpaBaseDao<Contract, Integer> {

    /**
     * 查询某征收项目下的没有选过房的基础信息（选房时间安排专用）
     *
     * @param zsProjectId
     * @return
     */
    public List<Contract> getContractListByZsProjectIdAndChooseHouseDateIsNotNull(String zsProjectIdStr) {
        StringBuffer hql = new StringBuffer(" from Contract c where  c.chooseHouseDate is not null and  c.chooseHouseDate != ''  ");
        hql.append(" and c.zsProject.id in (" + zsProjectIdStr + ") ");
        hql.append(" order by c.chooseHouseDate desc ");
        Map<String, Object> map = new HashMap<>();
        return this.findHql(hql.toString(), map);
    }
    /**
     * 根据协议id获取已选择的车位list
     *
     * @param azProjectId
     * @return
     */
    public PageBean<Contract> getContractList(HttpServletRequest request, String zsProjectIdStr) {
        Integer contractId = ParamUtils.getInt(request, "contractId", 0);
        Integer pageSize = ParamUtils.getInt(request, "size", 15);
        Integer pageNo = ParamUtils.getInt(request, "num", 1);
        String keyword = ParamUtils.getString(request, "keyword", "");

        StringBuffer hql = new StringBuffer(" c from Contract c inner join fetch c.zsProject zp where zp.id in " + zsProjectIdStr );
        hql.append(" and c.chooseHouseDate is not null and c.chooseHouseStatus =2 ");
        if (contractId != 0) {
            hql.append(" and c.id = " + contractId + "");
        }
        if (StringUtils.isNotEmpty(keyword)) {
            hql.append("and (c.name like '%" + keyword + "%' or c.chooseHouseNum like '%" + keyword + "%') ");
        }

        StringBuffer orderBy = new StringBuffer(" c.chooseHouseDate desc,c.chooseHouseNum");
        PageBean pageBean = new PageBean();
        pageBean.setPageSize(pageSize);
        pageBean.setPageNum(pageNo);
        Wrapper wrapper = new Wrapper(pageBean, hql, null, orderBy);
        return this.pageHql(wrapper);
    }

    /**
     * 测算协议列表
     * @param zsProjectIdStr
     * @param carNum
     * @param start
     * @param pageSize
     * @return
     */
    public List<Contract> getContractListByZsProjectId(String zsProjectIdStr,String likeStr,Integer start,Integer pageSize) {
        StringBuffer hql = new StringBuffer("from Contract c inner join fetch c.zsProject zs where c.zsProject.id in " + zsProjectIdStr);
        if (likeStr != "") {
            hql.append(" and (c.name like '%" + likeStr + "%' ");
            hql.append(" or c.chooseHouseNum like '%" + likeStr + "%' ");
            hql.append(" or c.contractCode like '%" + likeStr + "%') ");
        }
        hql.append(" ORDER by c.chooseHouseStatus,zs.id,c.chooseHouseNum,c.name  ");
        Map<String, Object> map = Maps.newLinkedHashMap();
        return this.findHqlLimit(hql.toString(), map,start,pageSize);
    }

    /**
     * 测算协议最大条数
     * @param zsProjectIdStr
     * @param carNum
     * @return
     */
    public Integer getContractListByZsProjectIdSize(String zsProjectIdStr,String likeStr) {
        StringBuffer hql = new StringBuffer("from Contract c inner join fetch c.zsProject zs where c.zsProject.id in " + zsProjectIdStr);
        if (likeStr != "") {
            hql.append(" and (c.name like '%" + likeStr + "%' ");
            hql.append(" or c.chooseHouseNum like '%" + likeStr + "%' ");
            hql.append(" or c.contractCode like '%" + likeStr + "%') ");
        }
        hql.append(" ORDER by c.chooseHouseStatus,zs.id,c.chooseHouseNum,c.name ");
        Map<String, Object> map = Maps.newLinkedHashMap();
        return this.findHql(hql.toString(), map).size();
    }
}
